package com.ahq.main;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import com.dalonedrau.vectors.Vector3;
import com.dalonedrau.jogl.engine.Action;
import com.dalonedrau.jogl.engine.Animation;
import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.engine.GlobalConsts;
import com.dalonedrau.jogl.engine.Interactive;
import com.dalonedrau.jogl.engine.Render;
import com.dalonedrau.jogl.opengl.View;
import com.dalonedrau.jogl.opengl.animation.AnimationSequenceObjectFactory;
import com.dalonedrau.jogl.opengl.sprite.BasicQuadSpriteObject;
import com.dalonedrau.jogl.opengl.sprite.BasicSpriteObject;
import com.dalonedrau.jogl.opengl.sprite.BasicTriangleSpriteObject;
import com.dalonedrau.jogl.opengl.sprite.GroupOfImages;
import com.dalonedrau.jogl.opengl.sprite.SpriteBase;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObject;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObjectFactory;
import com.dalonedrau.jogl.opengl.sprite.SpriteTileMap;
import com.dalonedrau.jogl.opengl.ui.Button;
import com.dalonedrau.jogl.opengl.ui.Label;
import com.dalonedrau.jogl.opengl.ui.OverworldMap;
import com.dalonedrau.jogl.opengl.ui.TextField;
import com.dalonedrau.jogl.opengl.ui.UIFactory;
import com.dalonedrau.jogl.opengl.ui.border.VXBorder;
import com.dalonedrau.jogl.opengl.ui.menu.MenuGroup;
import com.dalonedrau.jogl.opengl.ui.menu.MenuItem;
import com.dalonedrau.jogl.opengl.ui.menu.VerticalMenu;
import com.dalonedrau.jogl.opengl.ui.panel.PagePanel;
import com.dalonedrau.jogl.opengl.ui.panel.Panel;
import com.dalonedrau.jogl.opengl.ui.panel.PortraitPagePanel;
import com.dalonedrau.jogl.util.UtilityMethods;
import com.dalonedrau.pool.PooledStringBuilder;
import com.dalonedrau.pool.StringBuilderPool;
import com.dragonwarriors.player.DWCharCreationScreen;
import com.dragonwarriors.player.DWCharacter;
import com.dragonwarriors.player.DWConsts;
import com.dragonwarriors.player.DWPlayer;
import com.rpg.base.interactive.flyweight.BaseInteractiveObject;
import com.rpg.base.interactive.flyweight.InteractiveObject;
import com.rpg.dragonquest.player.DQCharacter;
import com.rpg.dragonquest.player.DQPCViewer;

/**
 * The title screen view.
 * @author Donald
 */
public final class TileMapTestView implements View {
	private Label label;
	private Button button;
	/** the title screen menu. */
	private MenuGroup		menu;
	/** the panel used to organize menu items. */
	private PagePanel		menuPanel;
	private TextField textfield;
	/**
	 * Initializes the start menu.
	 * @throws Exception if an error occurs
	 */
	private void initMenu() throws Exception {
		/*
		menu = new VerticalMenu();
		button = new Button("lunchds_18", "EXIT");
		button.setBorder(new SNESBorder());
		Point pt = GlobalMethods.centerComponentInFrame(button);
		button.setPosition(pt.x, pt.y, 0.0f);
		button.setVisible(true);
		button.addAction(new Action() {
			@Override
			public void performAction() {
				AHQApplet.getInstance().exit();
			}			
		});
		button.setSelectBorder(new H1SelectionBorder());
		button.setSelected(true);
		*/
		menu = new VerticalMenu();
		menu.setBorder(new VXBorder());
		MenuItem item = UIFactory.getInstance().getStandardMenuItem(
				"NEW GAME");
		item.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				//GameState.getInstance().nextState();
				// GameState.getInstance().setCurrentState(State.IN_PLAY);
			}
		});
		menu.addItem(item);
		MenuItem item2 = UIFactory.getInstance().getStandardMenuItem(
				"OUR STORY SO FAR...");
		item2.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				/*
				menuPanel.setVisible(false);
				scroller.moveTextToBottom();
				scroller.start();
				introPanel.setVisible(true);
				menuIntro.setVisible(false);
				*/
			}
		});
		menu.addItem(item2);
		MenuItem item3 = UIFactory.getInstance().getStandardMenuItem(
				"EXIT");
		item3.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				AHQApplet.getInstance().exit();
			}
		});
		menu.addItem(item3);
		// highlight the 1st index
		menu.highlightIndex(0);
		/*
		 * Sprite titleBanner = Factory.getInstance().getEmptySprite();
		 * titleBanner.setSize(Globals.TITLE_BANNER_WIDTH,
		 * Globals.TITLE_BANNER_HEIGHT); titleBanner.setImage( new ListImage(
		 * COBCanvasListener.getInstance().getIdTitleBanner())); Label lbl = new
		 * Label(titleBanner); Point pt = Globals.centerComponentInFrame(lbl);
		 * lbl.setPosition(new Vector3(pt.x, pt.y, 0.0f)); lbl.setVisible(true);
		 * menuPanel = new PortraitPagePanel(ComponentPortraitAlignment.CENTER);
		 * menuPanel.addChild(lbl); menuPanel.addChild(menu); pt =
		 * Globals.centerComponentInFrame(menuPanel); menuPanel.setPosition(new
		 * Vector3(pt.x, pt.y, 0.0f)); menuPanel.setVisible(true);
		 */
		menuPanel = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
		menuPanel.addChild(menu);
		Point pt = UtilityMethods.getInstance().centerComponentInFrame(menuPanel);
		menuPanel.setPosition(pt.x, pt.y, 0.0f);
		menuPanel.setVisible(true);
		
		textfield = new TextField(AHQConstants.getInstance().getFontH1(), 10);
		textfield.setBorder(new VXBorder());
		textfield.setPosition(pt.x, pt.y, 0.0f);
		textfield.setVisible(true);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleKeyEvent(final KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_ESCAPE:
			System.out.println("esc");
			AHQApplet.getInstance().exit();
			break;
		case KeyEvent.VK_UP:
			// menu.highlightPrevious();
			break;
		case KeyEvent.VK_DOWN:
			// menu.highlightNext();
			break;
		case KeyEvent.VK_ENTER:
			/*
			 * if (menu != null && menu.isVisible() && menu.getHighlighted() !=
			 * null) { menu.getHighlighted().onSelect(); } else if (scroller !=
			 * null && scroller.isVisible() && scroller.isScrolling()) {
			 * scroller.moveTextToTop(); } else if (menuIntro != null &&
			 * menuIntro.isVisible() && menuIntro.getHighlighted() != null) {
			 * menuIntro.getHighlighted().onSelect(); }
			 */
			break;
		case KeyEvent.VK_TAB:
			/*
			 * if (menu != null) { if (e.isShiftDown()) {
			 * menu.highlightPrevious(); } else { menu.highlightNext(); } }
			 */
			break;
		case KeyEvent.VK_BACK_SPACE:
			try {
				textfield.removeCharacter();
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
			break;
		case KeyEvent.VK_SPACE: 
			try {
				textfield.addCharacter(e.getKeyChar());
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
			break;
		default:
			if (Character.isLetterOrDigit(e.getKeyChar())) {
				try {
					textfield.addCharacter(e.getKeyChar());
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
			break;
		}
	}
	DQCharacter character;
	private void initViewer() {
		character = new DQCharacter();
		/*
		character.setName(new char[]{
				'G', 'r', 'o', 'm', 'a', 'r', 'k', 
				' ', 't', 'h', 'e', 
				' ', 'S', 'm', 'i', 't', 'h'
		});
		character.setGender(DWConsts.GENDER_MALE);
		character.setRank(1);
		character.setProfession(DWConsts.PROF_ELEMENTALIST_DARKNESS);
		character.setAttack(16);
		character.setDefence(7);
		character.setStrength(16);
		character.setMagicalAttack(0);
		character.setMagicalDefence(3);
		character.setReflexes(11);
		character.setHealthPoints(12);
		character.setMaxHealthPoints(12);
		character.setStealth(13);
		character.setEvasion(5);
		character.setPerception(5);
		*/
		//DWPlayer.getInstance().computeFullStats(character);
		/*
		PagePanel rowOne = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		rowOne.setBorder(new SNESBorder());
		Label name = 
			UIFactory.getInstance().getStandardH1Label(character.getName());
		// 5px margin on right
		name.setBorder(new EmptyBorder(0, 0, 0, 10));
		rowOne.addChild(name);
		Label gender = 
			UIFactory.getInstance().getStandardH1Label(
					DWConsts.GENDERS[character.getGender()]);
		// 5px margin on right
		gender.setBorder(new EmptyBorder(0, 0, 0, 10));
		rowOne.addChild(gender);
		Label rank = 
			UIFactory.getInstance().getStandardH1Label(
					DWPlayer.getInstance().getRankString(character));
		// 5px margin on right
		rank.setBorder(new EmptyBorder(0, 0, 0, 10));
		rowOne.addChild(rank);
		Label profession = 
			UIFactory.getInstance().getStandardH1Label(
					DWConsts.PROFESSIONS[character.getProfession()]);
		// 5px margin on right
		profession.setBorder(new EmptyBorder(0, 0, 0, 10));
		rowOne.addChild(profession);
		viewerPanel.addChild(rowOne);
		

		PagePanel rowTwo = new LandscapePagePanel(Panel.LANDSCAPE_TOP);
		PagePanel r2c1 = new LandscapePagePanel(Panel.LANDSCAPE_TOP);
		r2c1.setBorder(new SNESBorder());
		PagePanel r2c1p1 = new PortraitPagePanel(Panel.PORTRAIT_LEFT);
		r2c1p1.setBorder(new EmptyBorder(0, 0, 0, 5));
		{
			Label l = UIFactory.getInstance().getStandardH1Label("STRENGTH");
			r2c1p1.addChild(l);
		}
		{
			Label l = UIFactory.getInstance().getStandardH1Label("REFLEXES");
			r2c1p1.addChild(l);
		}
		{
			Label l = UIFactory.getInstance().getStandardH1Label("INTELLIGENCE");
			r2c1p1.addChild(l);
		}
		{
			Label l = UIFactory.getInstance().getStandardH1Label("PSYCHIC TALENT");
			r2c1p1.addChild(l);
		}
		{
			Label l = UIFactory.getInstance().getStandardH1Label("LOOKS");
			r2c1p1.addChild(l);
		}
		{
			Label l = UIFactory.getInstance().getStandardH1Label("HEALTH POINTS");
			r2c1p1.addChild(l);
		}
		r2c1.addChild(r2c1p1);
		
		PagePanel r2c1p2 = new PortraitPagePanel(Panel.PORTRAIT_LEFT);
		{
			Label l = UIFactory.getInstance().getStandardH1Label(
					character.getFullStrength());
			r2c1p2.addChild(l);
		}
		{
			Label l = UIFactory.getInstance().getStandardH1Label(
					character.getFullReflexes());
			r2c1p2.addChild(l);
		}
		{
			Label l = UIFactory.getInstance().getStandardH1Label(
					character.getFullIntelligence());
			r2c1p2.addChild(l);
		}
		{
			Label l = UIFactory.getInstance().getStandardH2Label(
					character.getFullPsychicTalent());
			l.setBorder(new DWStatsBorder(l));
			r2c1p2.addChild(l);
		}
		{
			Label l = UIFactory.getInstance().getStandardH2Label(
					character.getFullLooks());
			l.setBorder(new DWStatsBorder(l));
			r2c1p2.addChild(l);
		}
		{
			PooledStringBuilder sb =
				StringBuilderPool.getInstance().getStringBuilder();
			sb.append(character.getFullHealthPoints());
			sb.append('/');
			sb.append(character.getFullMaxHealthPoints());			
			Label l = UIFactory.getInstance().getStandardH1Label(sb.toString());
			sb.returnToPool();
			r2c1p2.addChild(l);
		}
		Panel test = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		r2c1.addChild(r2c1p2);
		
		rowTwo.addChild(r2c1);
		viewerPanel.addChild(rowTwo);
		*/
		Point pt = UtilityMethods.getInstance().centerComponentInFrame(
				DQPCViewer.getInstance());
		DWCharCreationScreen.getInstance().setPosition(pt.x, pt.y, 0.0f);
		DWCharCreationScreen.getInstance().setVisible(true);
		//DQPCViewer.getInstance().setPc(character);
		runInit = true;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleMouseOver(final float x, final float y) {
		map.handleMouseOver(x, y);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleMousePress(final MouseEvent e, final float x,
			final float y) {
		//this.menuPanel.handleMouseClick(x, y);
	}
	private BaseInteractiveObject eina;
	private BaseInteractiveObject parace;
	private BaseInteractiveObject sandra;
	private BaseInteractiveObject me;
	private BaseInteractiveObject sj;
	private BaseInteractiveObject arrow;
	private BaseInteractiveObject rect;
	boolean runInit = false;
	/**
	 * Initializes the {@link View}.
	 * @throws Exception if an error occurs
	 */
	private void init() throws Exception {
		map = new OverworldMap(160, 160);
		map.loadTileMap(Init.getInstance().loadTileMap("com/module/bp/dat/xml/bp_map_new.xml"));
		map.setPosition(0, 0, 0);
		this.runInit = true;
	}
	private OverworldMap map;
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		if (!runInit) {
			try {
				init();
			} catch (Exception ex) {
				AHQApplet.getInstance().fatalError(ex);
			}
		}
		//menuPanel.prepareForRendering();
		map.prepareForRendering();
		//Render.getInstance().registerEntity(parace.getSprite());
		/*
		Render.getInstance().registerEntity(eina.getSprite());
		Render.getInstance().registerEntity(parace.getSprite());
		Render.getInstance().registerEntity(sandra.getSprite());
		//Render.getInstance().registerEntity(arrow.getSprite());
		//Render.getInstance().registerEntity(rect.getSprite());
		/*
		Render.getInstance().registerEntity(sandra.getSprite());
		Render.getInstance().registerEntity(me.getSprite());
		Render.getInstance().registerEntity(sj.getSprite());
		*/
		
	}
}
